package com.demo.dialogdemo;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends Activity {

    private final int SINGLE_DIALOG = 101;
    private final int MULTI_DIALOG = 102;
    private final int ADAPTER_DIALOG = 103;
    private final int SELF_DIALOG = 104;

    private int[] images = new int[]{R.drawable.libai, R.drawable.nongyu, R.drawable.qingzhao, R.drawable.tiger};
    private String[] names = new String[]{"张三", "李四", "王五", "赵四"};
    private String[] petNames = new String[]{"(李白)", "(弄玉)", "(清照)", "(虎头)"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn1 = findViewById(R.id.btn1); //提示对话框
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // alertDialog
                AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
                b.setTitle("提示");
                b.setIcon(R.drawable.tools);
                b.setMessage("这是一个提示对话框");
                b.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "你点击了确定按钮", Toast.LENGTH_SHORT).show();
                    }
                });
                b.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "你点击了取消按钮", Toast.LENGTH_SHORT).show();
                    }
                });
                b.setNeutralButton("中立", null);
                b.create().show();
            }
        });
        Button btn2 = findViewById(R.id.btn2); // 列表对话框
        final TextView show = (TextView) findViewById(R.id.show);
        show.setText("对话框测试");
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder b = new AlertDialog.Builder(
                        MainActivity.this);
                b.setTitle("列表对话框");
                b.setIcon(R.drawable.tools);
                b.setItems(new String[]{"红色", "绿色", "蓝色", "黑色"},
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) { // TODO Auto-generated method
                                // stub
                                switch (which) {
                                    case 0:
                                        show.setTextColor(Color.RED);
                                        break;
                                    case 1:
                                        show.setTextColor(Color.GREEN);
                                        break;
                                    case 2:
                                        show.setTextColor(Color.BLUE);
                                        break;
                                    default:
                                        show.setTextColor(Color.BLACK);
                                        break;
                                }
                            }
                        });

                b.setPositiveButton("确定", null);
                b.create().show();
            }
        });
        Button btn3 = findViewById(R.id.btn3);
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog(SINGLE_DIALOG);
            }
        });

        Button btn4 = findViewById(R.id.btn4);
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog(MULTI_DIALOG);
            }
        });

        Button btn5 = findViewById(R.id.btn5);
        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog(ADAPTER_DIALOG);
            }
        });

        Button btn6 = findViewById(R.id.btn6);
        btn6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog(SELF_DIALOG);
            }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        final TextView show = (TextView) findViewById(R.id.show);
        show.setText("测试对话框");
        AlertDialog.Builder b = new AlertDialog.Builder(
                MainActivity.this);
        b.setTitle("列表对话框");
        b.setIcon(R.drawable.tools);
        switch (id) {
            case SINGLE_DIALOG:
                b.setSingleChoiceItems(new String[]{"红色", "绿色", "蓝色", "黑色"}, 0,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                                switch (which) {
                                    case 0:
                                        show.setTextColor(Color.RED);
                                        break;
                                    case 1:
                                        show.setTextColor(Color.GREEN);
                                        break;
                                    case 2:
                                        show.setTextColor(Color.BLUE);
                                        break;

                                    default:
                                        show.setTextColor(Color.BLACK);
                                        break;
                                }
                            }
                        });
                b.setPositiveButton("确定", null);
                return b.create();
            case MULTI_DIALOG:
                final String colors[] = new String[]{"红色", "绿色", "蓝色", "黑色"};
                final boolean states[] = new boolean[]{false, false, false, true};
                b.setMultiChoiceItems(colors, states,
                        new DialogInterface.OnMultiChoiceClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which,
                                                boolean isChecked) {
                                // TODO Auto-generated method stub
                                String txt = "你选择的颜色是：";
                                for (int i = 0; i < states.length; i++)
                                    if (states[i])
                                        txt += colors[i] + " ";
                                show.setText(txt);
                            }
                        });
                b.setPositiveButton("确定", null);
                return b.create();
            case ADAPTER_DIALOG:
                ArrayList<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
                for (int i = 0; i < names.length; i++) {
                    Map<String, Object> item = new HashMap<String, Object>();
                    item.put("headImage", images[i]);
                    item.put("name", names[i]);
                    item.put("petName", petNames[i]);
                    listItems.add(item);
                }
                final SimpleAdapter sa = new SimpleAdapter(this, listItems, R.layout.list_item,
                        new String[]{"headImage", "name", "petName"},
                        new int[]{R.id.head_image, R.id.name, R.id.pet_name});
                b.setAdapter(sa, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "你要跟" + names[which] + "聊天", Toast.LENGTH_LONG).show();
                    }
                });
                b.setPositiveButton("确定", null);
                return b.create();
            case SELF_DIALOG:
                View layout = MainActivity.this.getLayoutInflater().inflate(R.layout.login, null);
                b.setTitle("登陆");
                b.setView(layout);
                b.setPositiveButton("登陆", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 登陆认证
                        //.....
                    }
                });
                b.setNegativeButton("取消", null);
                b.create().show();

            default:
                break;
        }
        return super.onCreateDialog(id);
    }
}
